#include <stdio.h>
#include "localsearcher.h"
#include "EasyBMP.h"
#include<Windows.h>
#include "priorityqueueimpl.h"

bool isStart(RGBApixel pixel){
	return pixel.Blue==0 && pixel.Green==0 && pixel.Red==255;
}

bool isEnd(RGBApixel pixel){
	return pixel.Blue==255 && pixel.Green==0 && pixel.Red==0;
}

bool isWalkable(RGBApixel pixel){
	return pixel.Blue==255 && pixel.Green==255 && pixel.Red==255;
}

int main() {
	printf("Start\n");
	try{
	BMP bmp;
	bmp.ReadFromFile("map.bmp");
	int width=bmp.TellWidth();
	int height=bmp.TellHeight();
	bool**grid=new bool*[height];
	Point*start=new Point(-1,-1);
	Point*end=new Point(-1,-1);
	for(int j=0;j<height;j++){
		grid[j]=new bool[width];
		for(int i=0;i<width;i++){
			RGBApixel pixel=bmp.GetPixel(i,j);
			bool walkable=true;
			if(isStart(pixel)){
				start->setX(i);start->setY(j);
			}else if(isEnd(pixel)){
				end->setX(i);end->setY(j);
			}else if(!isWalkable(pixel)){
				walkable=false;
			}

			grid[j][i]=walkable;
		}
	}
	
	if(start->getX()==-1 && start->getY()==-1){
		printf("start Red pixel is not defined on the map");
		return 0;
	}

	if(end->getX()==-1 && end->getY()==-1){
		printf("end Blue pixel is not defined on the map");
		return 0;
	}
	shared_ptr<Point>endPtr=shared_ptr<Point>(end);
	shared_ptr<Point>startPtr=shared_ptr<Point>(start);
		printf("iteration\n");
		GridSearch search(grid,width,height,endPtr);
		long l=GetTickCount();
		std::vector<shared_ptr<Point>>result=search.compute(startPtr);
		long l2=GetTickCount();
		printf("result = %d. time=%d\n",result.size(), l2-l);
		RGBApixel p;
		p.Red=255;
		p.Green=255;
		p.Blue=0;
	}catch(char*t){
		printf("Exception ex=%s\n",t);
	}
	return 0;
}
